(function ($) {
	$.fn.fadeTransition = function(options) {
	  var options = $.extend({pauseTime: 5000, transitionTime: 1000,left:'0',normal:'1'}, options);
	  var transitionObject;

	  Trans = function(obj) {
		var timer = null;
		var current = 0;
		if(options.normal == "2")
		{
		var els = $("> *", obj).css("display", "none").css("left", options.left).css("top", "0").css("position", "absolute");
		$(obj).css("position", "absolute");
		
		}else{
			var els = $("> *", obj).css("display", "none").css("left", options.left).css("top", "0").css("position", "absolute");
		$(obj).css("position", "relative");
		}
		$(els[current]).css("display", "block");

		function transition(next) {
		
		
		$(els[current]).fadeOut(options.transitionTime);
		  $(els[next]).fadeIn(options.transitionTime);

		  if(options.normal == "1")
		  {
		 	 var showimgObj = $(els[next]).find('img').get(0);
		 	 if(showimgObj.width > 732)
				showimgObj.width = 732;
		  	if(showimgObj.height < 488)
		 	 {
		 	 	
		  		var margintop = (518 - showimgObj.height)/2;
		 	 //	margintop -= 20;
		  		$(showimgObj).css('margin-top',margintop+"px");	
		  	}
		  }
		  if(options.normal == "2")
		  {
		 	 if( page_fullscreen.img_size_table[next] == undefined)
		 	 {
		  		var showimgObj = $(els[next]);
		  		$.log('im-w:'+ showimgObj.width());
		  		$.log('im-h:'+showimgObj.height());
		  		 var img_size = {'width' : showimgObj.width(), 'height' : showimgObj.height()};
       	 		 page_fullscreen.img_size_table[next] = img_size;
       	 	 }
       	 	 
       	 	 //setTimeout( page_fullscreen.locateBigImage,500);
       	 	 page_fullscreen.locateBigImage();
       	 	/*
       	 	$(window).trigger('resize');
       	 	setTimeout(function(){$(window).trigger('resize');},500);
       	 	setTimeout(function(){$(window).trigger('resize');},1000);
       	 	setTimeout(function(){$(window).trigger('resize');},2000);
       	 	setTimeout(function(){$(window).trigger('resize');},5000);
       	 	*/
		  }
	
		  current = next;
		 // cue();
		};

		function cue() {
		/*
		if(options.normal == "2")
		{
			 if ($("> * img", obj).length < 2) return false;
		}else{
			 if ($("> *", obj).length < 2) return false;
		}*/
		  if ($("> *", obj).length < 2) return false;
		  if (timer) clearTimeout(timer);
		  timer = setTimeout(function() { transition((current + 1) % els.length | 0)} , options.pauseTime);
		};
		
		this.showItem = function(item) {
		  if (timer) clearTimeout(timer);
		  transition(item);
		};

		cue();
	  }

	  this.showItem = function(item) {
		transitionObject.showItem(item);
	  };

	  return this.each(function() {
		transitionObject = new Trans(this);
	  });
	}

  })(jQuery);

  var page_transition = {
	tr: null,
	totalItem:0,
	currentItem:0,
	pre_btn:null,
	next_btn:null,
	init: function(option) {
	  page_transition.tr = $(".normal.sarea").fadeTransition({pauseTime: 700000000, transitionTime: 1500,left:option.left});
	  $(".normal .navicontroller a").each(function(idx) {
	  		page_transition.totalItem++;
			$(this).click(function() {
				page_transition.tr.showItem(idx); 
				page_transition.currentItem = idx;
				page_transition.update_image_description();
				return false;
				}
			)
	  });
	  //page_transition.addctrbtn();
	  //$($(".navicontroller a").get(0)).trigger('click');
	},
	addctrbtn:function(){
		//add prev button
		page_transition.pre_btn = $("<div class='bx-prev'><a href='#' onclick='javascript:return false;'></a></div>");
		
		 $(".sarea").parent().append(page_transition.pre_btn);
		 page_transition.pre_btn.bind('click',function(){
		 	if(page_transition.currentItem == 0)
		 		page_transition.currentItem = page_transition.totalItem;
		 	page_transition.currentItem--;
		 	page_transition.tr.showItem(page_transition.currentItem); 
		 	page_transition.update_image_description();
		 });
		 //add next button
		 
		 page_transition.next_btn = $("<div class='bx-next'><a href='#' onclick='javascript:return false;'></a></div>");
			 $(".sarea").parent().append(page_transition.next_btn);
		 page_transition.next_btn.bind('click',function(){
		 	if(page_transition.currentItem == (page_transition.totalItem -1))
		 		page_transition.currentItem = -1;
		 	page_transition.currentItem++;
		 	page_transition.tr.showItem(page_transition.currentItem); 
		 	page_transition.update_image_description();
		 });
		 page_transition.locationbtn();
	},
	locationbtn:function(){
	var position = $(".sarea").position();
			 page_transition.pre_btn.css("left",position.left - 0);
		 page_transition.pre_btn.css("top",position.top + $(".sarea").height()/2);
 page_transition.next_btn.css("left",position.left + $(".sarea").width() -30);
		 page_transition.next_btn.css("top",position.top + $(".sarea").height()/2);
	
	},
	update_image_description:function(){
		//update image description
		if(typeof global_image_description_table != "undefined")
	 		$('#contactf-img2-normal').text(global_image_description_table[page_transition.currentItem]);
	},
	show: function(idx) {
	  if (page_transition.tr.timer) clearTimeout(page_transition.tr.timer);
	  page_transition.tr.showItem(idx);
	  
	}
  };

var global_navicontroller_bar_size = 0;    
$(function(){
    /*$(window).resize(function() {
  			page_transition.locationbtn();
  			if(page_fullscreen)
  			{
  				page_transition_full.locationbtn();
  			}
	});*/
	var navicontroller_bar_item_count = $('.normal .navicontroller a').size();
	global_navicontroller_bar_size = navicontroller_bar_item_count * 91;
	$('.normal .navicontroller_next').bind('click',function() {
		var currentLeft = $('.normal .navicontroller').css('margin-left');
        currentLeft = currentLeft.substring(0, currentLeft.indexOf('px'));
		currentLeft = parseInt(currentLeft, 10);
		currentLeft -= 91;
		if(global_navicontroller_bar_size + currentLeft - 880 < 0)
			return false;
		$('.normal .navicontroller').css('margin-left', currentLeft + 'px');
		return false;
	});
	$('.normal .navicontroller_prev').bind('click',function(){
		var currentLeft = $('.normal .navicontroller').css('margin-left');
        currentLeft = currentLeft.substring(0, currentLeft.indexOf('px'));
		currentLeft = parseInt(currentLeft, 10);
		currentLeft += 91;
		if( currentLeft > 0)
			return false;
		$('.normal .navicontroller').css('margin-left',currentLeft+'px');
		return false;
	});
});

  
